package com.zhiwei.msf.gateway.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.zhiwei.msf.common.constant.DatabaseConstants;
import com.zhiwei.msf.common.service.CacheService;
import com.zhiwei.msf.gateway.dao.entity.WhiteList;
import com.zhiwei.msf.gateway.dao.entity.WhiteListExample;
import com.zhiwei.msf.gateway.dao.mapper.WhiteListMapper;
import com.zhiwei.msf.gateway.service.WhiteListService;

import lombok.extern.slf4j.Slf4j;

/**
 * function
 * author zhiwei.yang
 * time 2020-2-28-17:00
 */
@Service
@Slf4j
public class WhiteListServiceImpl implements WhiteListService {

    @Autowired
    private WhiteListMapper whiteListMapper;

    @Autowired
    private CacheService cacheService;

    @Value("${spring.application.name:}")
    private String app;

    /**
     * 白名单过期时间，间隔30s数据库更新一次
     */
    @Value("${white.list.expire.time:30}")
    private Long WHITE_LIST_EXPIRED_TIME;

    /**
     * 白名单缓存键
     */
    private final String WHITE_LIST_CACHE_KEY = "whiteList";

    @Override
    public List<String> queryWhiteList(WhiteListExample whiteListExample) {
        List<WhiteList> whiteLists = whiteListMapper.selectByExample(whiteListExample);
        if (CollectionUtils.isEmpty(whiteLists)) {
            log.info("白名单查询为空whiteListExample:{}", whiteListExample);
            return Collections.emptyList();
        }
        List<String> WhiteUrlList = whiteLists.stream().map(WhiteList::getUrlPath).collect(Collectors.toList());
        log.info("查询白名单信息whiteListExample：{}, WhiteUrlList:{}", JSON.toJSONString(whiteListExample), whiteLists);
        return WhiteUrlList;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String> queryValidWhiteList() {
		List<String> whiteList = (List<String>) cacheService.getCacheObject(app, WHITE_LIST_CACHE_KEY);
        if (null != whiteList) {
            return whiteList;
        }
        WhiteListExample whiteListExample = new WhiteListExample();
        WhiteListExample.Criteria criteria = whiteListExample.createCriteria();
        criteria.andEnableFlagEqualTo(DatabaseConstants.ENABLE);
        whiteList = this.queryWhiteList(whiteListExample);
        cacheService.putCacheObject(app, WHITE_LIST_CACHE_KEY, whiteList, WHITE_LIST_EXPIRED_TIME);
        log.info("数据库白名单数据：{}", whiteList);
        return whiteList;
    }

    @Override
    public boolean addWhiteList(WhiteList whiteList) {
        if (null == whiteList) {
            log.info("新增白名单参数为空");
            return false;
        }
        int result = whiteListMapper.insert(whiteList);
        cacheService.removeCacheObject(app, WHITE_LIST_CACHE_KEY);
        log.info("添加白名单成功：{}", whiteList);
        return result == 1;
    }
}